package com.demo.elastic;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Map;

public class ExporterProperties {

    private ElasticConfig source;

    private ElasticConfig target;

    private ExporterProperties() {  }



    static ExporterProperties parse(Map<String, String> params) {
        ExporterProperties properties = new ExporterProperties();
        properties.source = new ElasticConfig();
        properties.target = new ElasticConfig();

        String sourceClusterName = params.getOrDefault("source.cluster.name", ElasticConfig.DEFAULT_CLUSTER_NAME);
        params.remove("source.cluster.name", sourceClusterName);
        properties.source.clusterName = sourceClusterName;

        String sourceClusterNodes = params.getOrDefault("source.cluster.nodes", null);
        params.remove("source.cluster.nodes", sourceClusterNodes);
        properties.source.clusterNodes = sourceClusterNodes;

        String targetClusterName = params.getOrDefault("target.cluster.name", ElasticConfig.DEFAULT_CLUSTER_NAME);
        params.remove("target.cluster.name", targetClusterName);
        properties.target.clusterName = targetClusterName;

        String targetClusterNodes = params.getOrDefault("target.cluster.nodes", null);
        params.remove("", targetClusterNodes);
        properties.target.clusterNodes = targetClusterNodes;

        String sourcePropertiesPrefix = "source.properties.", targetPropertiesPrefix = "target.properties.";

        params.forEach((key, value) -> {
            if (StringUtils.startsWith(key, sourcePropertiesPrefix)) {
                properties.source.properties.put(StringUtils.substring(key, sourcePropertiesPrefix.length()), value);
            } else if (StringUtils.startsWith(key, targetPropertiesPrefix)) {
                properties.target.properties.put(StringUtils.substring(key, targetPropertiesPrefix.length()), value);
            }
        });
        return properties;
    }

    public ElasticConfig getSource() {
        return source;
    }

//    public void setSource(ElasticConfig source) {
//        this.source = source;
//    }

    public ElasticConfig getTarget() {
        return target;
    }

//    public void setTarget(ElasticConfig target) {
//        this.target = target;
//    }

    public static class ElasticConfig {

        public static final String DEFAULT_CLUSTER_NAME = "elasticsearch";

        public static final Map<String, String> DEFAULTS = new HashMap<>();
        static {

        }

        private String clusterName;
        private String clusterNodes;
        private Map<String, String> properties = new HashMap<>();

        public String getClusterName() {
            return clusterName;
        }

        public void setClusterName(String clusterName) {
            this.clusterName = clusterName;
        }

        public String getClusterNodes() {
            return clusterNodes;
        }

        public void setClusterNodes(String clusterNodes) {
            this.clusterNodes = clusterNodes;
        }

        public Map<String, String> getProperties() {
            return properties;
        }

        public void setProperties(Map<String, String> properties) {
            this.properties = properties;
        }
    }
}
